package com.mind.adjust.service.impl;

import java.util.List;

import com.mind.common.exception.ServiceException;
import com.mind.common.utils.DateUtils;
import com.mind.common.utils.StringUtils;
import com.mind.customer.mapper.AjReviewConfigMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mind.system.domain.AjReviewConfig;
import com.mind.adjust.service.IAjReviewConfigService;

/**
 * 智审配置Service业务层处理
 *
 * @author today
 * @date 2024-09-03
 */
@Service
public class AjReviewConfigServiceImpl implements IAjReviewConfigService
{
    private static final Logger log = LoggerFactory.getLogger(AjReviewConfigServiceImpl.class);
    @Autowired
    private AjReviewConfigMapper ajReviewConfigMapper;

    /**
     * 查询智审配置
     *
     * @param id 智审配置主键
     * @return 智审配置
     */
    @Override
    public AjReviewConfig selectAjReviewConfigById(Long id)
    {
        return ajReviewConfigMapper.selectAjReviewConfigById(id);
    }

    /**
     * 查询智审配置列表
     *
     * @param ajReviewConfig 智审配置
     * @return 智审配置
     */
    @Override
    public List<AjReviewConfig> selectAjReviewConfigList(AjReviewConfig ajReviewConfig)
    {
        return ajReviewConfigMapper.selectAjReviewConfigList(ajReviewConfig);
    }

    /**
     * 新增智审配置
     *
     * @param ajReviewConfig 智审配置
     * @return 结果
     */
    @Override
    public int insertAjReviewConfig(AjReviewConfig ajReviewConfig)
    {
        ajReviewConfig.setCreateTime(DateUtils.getNowDate());
        return ajReviewConfigMapper.insertAjReviewConfig(ajReviewConfig);
    }

    /**
     * 修改智审配置
     *
     * @param ajReviewConfig 智审配置
     * @return 结果
     */
    @Override
    public int updateAjReviewConfig(AjReviewConfig ajReviewConfig)
    {
        ajReviewConfig.setUpdateTime(DateUtils.getNowDate());
        return ajReviewConfigMapper.updateAjReviewConfig(ajReviewConfig);
    }

    /**
     * 批量删除智审配置
     *
     * @param ids 需要删除的智审配置主键
     * @return 结果
     */
    @Override
    public int deleteAjReviewConfigByIds(Long[] ids)
    {
        return ajReviewConfigMapper.deleteAjReviewConfigByIds(ids);
    }

    /**
     * 删除智审配置信息
     *
     * @param id 智审配置主键
     * @return 结果
     */
    @Override
    public int deleteAjReviewConfigById(Long id)
    {
        return ajReviewConfigMapper.deleteAjReviewConfigById(id);
    }

    @Override
    public String importData(List<AjReviewConfig> configList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(configList) || configList.size() == 0)
        {
            throw new ServiceException("导入配置数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (AjReviewConfig config : configList)
        {
            try
            {
                AjReviewConfig tmp = null;
                // 验证是否存在
                AjReviewConfig query = new AjReviewConfig();
                query.setRuleDetailId(config.getRuleDetailId());
                List<AjReviewConfig> lsConfig = ajReviewConfigMapper.selectAjReviewConfigList(query);
                if (lsConfig.size()==0)
                {
                    tmp = config;
                    tmp.setCreateTime(DateUtils.getNowDate());
                    tmp.setAddUser(operName);
                    ajReviewConfigMapper.insertAjReviewConfig(tmp);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、规则明细名称 " + tmp.getRuleDetailName() + " 导入成功");
                }
                else if (updateSupport)
                {
                    tmp = lsConfig.get(0);
                    config.setId(tmp.getId());
                    config.setUpdateTime(DateUtils.getNowDate());
                    ajReviewConfigMapper.updateAjReviewConfig(config);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、规则明细名称 " + tmp.getRuleDetailName() + " 更新成功");
                }
                else
                {
                    tmp = lsConfig.get(0);
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、规则明细名称 " + tmp.getRuleDetailName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + config.getRuleDetailName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
